import {Basebio} from "./basebio";
import index from "strip-ansi";
import {bulid_actions} from "../commdata";

export class Building extends Basebio{
    buildtype:string="";
    builddelay:number=200;
    buildindex:number=0;

    isCompleteBuild:boolean=false;   //建筑状态
    isCompleteProduce:boolean=false; //生产状态

    objectCount:number=0;  //增加人口数

    produceindex:number=0;
    producedelay:number=50;
    img:any=new Image();

    isSelect:boolean=false;

    constructor(_name:string,_x:number,_y:number,
                _buildtype:string,_live:number,_livelimit:number,
                _ratewidth:number,_rateheight:number,_team:number) {
        super();
        this.name = _name;
        this.x = _x;
        this.y = _y;
        this.buildtype = _buildtype;
        this.live = _live;
        this.livelimit = _livelimit;
        this.ratewidth=_ratewidth;
        this.rateheight=_rateheight;
        this.img.src=bulid_actions[0].imgsrc;
        if(_ratewidth==0)
           this.ratewidth=this.img.width;
        if(_rateheight==0)
          this.rateheight=this.img.height;
        this.team=_team;

    }

    builddraw=(ptctx:any)=>{
        if(this.isSelect==false)
           ptctx.drawImage(this.img,0,0,this.img.width,this.img.height,this.x,this.y,this.ratewidth,this.rateheight);
        else
        {
            ptctx.beginPath();
            ptctx.strokeStyle="green";
            ptctx.clearRect(this.x-5, this.y, this.ratewidth+2, this.rateheight+2);
            ptctx.rect(this.x-5, this.y, this.ratewidth+2, this.rateheight+2);
            ptctx.stroke();
            ptctx.drawImage(this.img,0,0,this.img.width,this.img.height,this.x,this.y,this.ratewidth,this.rateheight);
        }
    }

    actiondraw=(ptctx:any)=>{
        if(this.buildindex<this.builddelay) {

            //ptctx.drawImage(this.img, 0, 0, this.img.width, this.img.height, this.x, this.y, this.img.width, this.img.height);
            ptctx.globalAlpha = 0.4;
            ptctx.fillStyle = "#CC6633";
            ptctx.fillRect(this.x, this.y, this.img.width, this.img.height);
            ptctx.strokeRect(this.x, this.y, this.img.width, this.img.height);
            ptctx.globalAlpha = 1;
            ptctx.font = "oblique small-caps bold 12px Arial";
            ptctx.fillStyle = "yellow";
            this.buildindex=this.buildindex+1;
            ptctx.fillText("建造中："+(this.buildindex*100/1000).toString()+"/"+(this.builddelay*100/1000).toString(), this.x+this.img.width/2-20, this.y+this.img.height/2-15);
        }
        else {
            this.isCompleteBuild = true;
            if (this.produceindex < this.producedelay) {
                ptctx.drawImage(this.img, 0, 0, this.img.width, this.img.height, this.x, this.y, this.img.width, this.img.height);
                ptctx.font = "oblique small-caps bold 12px Arial";
                ptctx.fillStyle = "yellow";
                this.produceindex = this.produceindex + 1;
                ptctx.fillText("生产中：" + (this.produceindex*100/1000).toString()+"/"+(this.producedelay*100/1000).toString(), this.x + this.img.width / 2 - 20, this.y + this.img.height / 2 - 15);
            } else {
                 this.isCompleteProduce=true;
            }
        }
    }
}